package com.nobuyme.box.toy;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nobuyme.box.R;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.bean.AgeListEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 */
public class ToyRentFragment extends SwipeListFragment {

    private int mPage = 0;
    ToyRentListAdapter toyRentListAdapter;

    public ToyRentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        clearDecorations();
        setPullToRefreshEnable(false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {

        toyRentListAdapter = new ToyRentListAdapter(getActivity());
        return toyRentListAdapter;
    }


    private ListDataRequestListener<AgeListEntity> mResponceListener = new ListDataRequestListener<>(this, AgeListEntity.class);

    @Override
    public void onStartLoading() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new HttpRequest(getContext());
            UrlParams params = new UrlParams();
            request.get(UrlCenter.AGE_LIST, params, mResponceListener);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        List<AgeListEntity> o = toyRentListAdapter.getData();
        Log.e("mIdmId", o.get(position).getId() + "");
        ToyListActivity.start(getActivity(), o, o.get(position).getId(),position);
    }


    public class ToyRentListAdapter extends RecyclerViewBaseAdapter<AgeListEntity> {



        public ToyRentListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new ToyRentHolder(setAdapterViewItem(parent, R.layout.toy_rent_list));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder ItemHolder, int position) {
            ToyRentHolder holder = (ToyRentHolder) ItemHolder;
            AgeListEntity ageListEntity = getData().get(position);
            holder.mTvAgeList.setText(ageListEntity.getName());
            Glide.with(getActivity()).load(ageListEntity.getPic()).into(holder.mIvAgeList);

        }

        class ToyRentHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv_age_list)
            TextView mTvAgeList;
            @Bind(R.id.iv_age_list)
            ImageView mIvAgeList;
            public ToyRentHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }

}
